//
//  Pet.swift
//  hello
//
//  Created by 赵康 on 2024/10/10.
//

import Foundation
import Vapor
import Fluent

struct PetMigration: AsyncMigration {
    func prepare(on database: any Database) async throws {
			let animal = try await database.enum("animals")
            .case("dog")
            .case("cat")
            .create()
        
			try await database.schema(Pet.schema)
        
            .id()
            .field("name", .string, .required)
            .field("type", animal, .required)
            .create()
    }
    
    func revert(on database: any Database) async throws {
        try await database.enum("animals").delete() // 删除枚举类型
			try await database.schema(Pet.schema).delete()
    }
}

/// 杜绝出现硬编码的情况, 通过定义一个枚举类型来代替字符串;
/// 或者使用结构体来代替字符串, 以避免硬编码;

/*
 extension UserModel {
	 struct FieldKeys {
	 static var name: FieldKey { "姓氏" }
	 static var lastname: FieldKey { "名称" }
	 static var username: FieldKey { "用户名" }
	}
}
 let keys: UserModel.FieldKeys.Type = UserModel.FieldKeys.self
 */
